﻿/// <reference path="../jquery-1.9.1.js" />
/// <reference path="../referraltracker.js" />
/// <reference path="sitedialog.js" />

referraltracker.admin.index = (function () {

    var _id = null;
    var _dialog = new referraltracker.dialogs.sitedialog();

    function ShowAddDialog() {
        _dialog.initialize(function () {
            _dialog.show();
        });

        _dialog.onSave(function (data) {
            SaveAddedData(data);
        });
    }

    function SaveAddedData(raw) {
        var data = {
            Name: raw.name,
            RootUrl: raw.url,
            ReferralLinkPattern: raw.pattern
        };

        $.ajax({
            url: '/admin/AddSite',
            type: 'POST',
            data: data,
            dataType: 'json',
            success: OnSaveAddedData
        });
    }

    function OnSaveAddedData(result) {

        if (result.success) {
            var data = result.data;
            var newRowTemplate = "\
            <tr id='row_{id}'>\
                <td><span id='namecol_{id}'>{name}</span></td>\
                <td><span id='urlcol_{id}'>{url}</span></td>\
                <td><a href='javascript:referraltracker.admin.index.deleteSite({id})'>del</a></td>\
                <td><a href='javascript:referraltracker.admin.index.editSite({id})'>edit</a></td>\
            </tr>\
            ";
            var newRowHtml = newRowTemplate
                                .replace(/{id}/g, data.Id)
                                .replace("{name}", data.Name)
                                .replace("{url}", data.RootUrl);

            $("#sitelist").append(newRowHtml);
        }
        else {
            alert(result.errMsg);
        }

    }

    function RetrieveData(id) {
        var data = {
            Id: id
        };

        _id = id;
        $.ajax({
            url: '/admin/GetSite',
            type: 'POST',
            data: data,
            dataType: 'json',
            success: OnRetrieveData
        });
    }

    function OnRetrieveData(result) {
        var data = {
            mode: "update",
            name: result.Name,
            url: result.RootUrl,
            pattern: result.ReferralLinkPattern
        };

        ShowEditDialog(data);
    }

    function ShowEditDialog(data) {
        _dialog.initialize(function () {
            _dialog.show(data);
        });

        _dialog.onSave(function (data) {
            SaveUpdatedData(data);
        });
    }

    function SaveUpdatedData(raw) {
        var data = {
            Id: _id,
            Name: raw.name,
            RootUrl: raw.url,
            ReferralLinkPattern: raw.pattern
        };

        $.ajax({
            url: '/admin/UpdateSite',
            type: 'POST',
            data: data,
            dataType: 'json',
            success: OnSaveUpdatedData
        });
    }

    function OnSaveUpdatedData(result) {
        if (result.success) {
            var data = result.data;
            var labelName = $("#namecol_" + _id.toString());
            var labelUrl = $("#urlcol_" + _id.toString());

            labelName.text(data.Name);
            labelUrl.text(data.RootUrl);
        }
        else {
            alert(result.errMsg);
        }
    }

    function DeleteSite(id) {
        var ans = confirm("Are you sure you want to delete this data?");

        if (ans) {
            var data = {
                Id: id
            };

            _id = id;
            $.ajax({
                url: '/admin/DeleteSite',
                type: 'POST',
                data: data,
                dataType: 'json',
                success: OnDeleteSite
            });
        }
    }

    function OnDeleteSite(result) {
        if (result.success) {
            var row = $("#row_" + _id.toString());

            row.remove();
        }
        else {
            alert(result.errMsg);
        }
    }

    var _instance = {
        addSite: ShowAddDialog,
        editSite: function (id) {
            RetrieveData(id);
        },
        deleteSite: function (id) {
            DeleteSite(id);
        }
    };

    return _instance;
})();